import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import mglearn
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split

class solution:
    def datareading(self):
        pd.set_option("display.max_columns",1000000)
        pd.set_option('display.width',10000)
        self.data=pd.read_csv('sonar.csv')
        print(self.data)
    def datasplit(self):
        self.target=self.data.iloc[:,-1].copy()
        self.dataset=self.data.iloc[:,0:self.data.shape[1]-1].copy()
        print(self.target)
        print(self.dataset)
        self.x_train,self.x_test,self.y_train,self.y_test=train_test_split(self.dataset,self.target,stratify=self.target,random_state=42)
    def treeapply(self):
        tree=DecisionTreeClassifier(random_state=0)
        tree.fit(self.x_train,self.y_train)
        self.tree=tree
        print("Accuracy on training set:{:.3f}".format(tree.score(self.x_train,self.y_train)))
        print("Accuracy on test set:{:.3f}".format(tree.score(self.x_test,self.y_test)))
    def look(self):
        from sklearn.tree import export_graphviz
        export_graphviz(self.tree,out_file="tree.dot",class_names=["R","L"],impurity=False,filled=True)
        import graphviz
        with open("tree.dot") as f:
            dot_graph=f.read()
        graphviz.Source(dot_graph)
        plt.show()



s=solution()
s.datareading()
s.datasplit()
s.treeapply()
s.look()